package net.k3rnel.abugadro;

import net.k3rnel.abugadro.Project.Projects;
import net.k3rnel.abugadro.gcodes.beans.Comment.Comments;
import net.k3rnel.abugadro.gcodes.beans.CommentUpdate.CommentUpdates;
import net.k3rnel.abugadro.gcodes.beans.GoogleCode.GCode;
import net.k3rnel.abugadro.gcodes.beans.Issue.Issues;
import net.k3rnel.abugadro.gcodes.beans.Label.Labels;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

public class ProjectsDB {

	private ProjectsDB () {
	}

	private static final ProjectsDB instance = new ProjectsDB ();

	public static ProjectsDB  getInstance() {
		return instance;
	}

	public int addNewProject(ContentResolver contentResolver, String name, int type) {
		ContentValues contentValue = new ContentValues();
		contentValue.put(Projects.NAME, name);
		contentValue.put(Projects.TYPE, type);
		contentResolver.insert(Projects.CONTENT_URI, contentValue);
		
		contentValue = new ContentValues();
		final int gcodeId = isProjectInDB(contentResolver, name, type);
		contentValue.put(GCode.PROJECT_ID, gcodeId);
		contentResolver.insert(GCode.CONTENT_URI, contentValue);
		return gcodeId;
	}

	public int getProjectTypeById(ContentResolver contentResolver, long id) {
		int ret = 0;
		Cursor cursor = contentResolver.query(Projects.CONTENT_URI, null, Projects._ID + "='" + id + "'", null, null);
		if (null != cursor && cursor.moveToNext()) {
			int index = cursor.getColumnIndex(Projects.TYPE);
			ret = cursor.getInt(index);
		}
		cursor.close();
		return ret;
	}
	public Cursor getIssueChangesByIssueId(ContentResolver contentResolver, int gcodeId,int issueId, int commentId) {
		String[] projection = {CommentUpdates.CONTENT,CommentUpdates.TYPE,CommentUpdates.POSITIVE};
		Cursor cursor = contentResolver.query(CommentUpdates.CONTENT_URI, projection, CommentUpdates.GCODE_ID + "='" + gcodeId + "' AND "+CommentUpdates.ISSUE_ID + "='"+issueId+"' AND "+CommentUpdates.COMMENT_ID + "='"+commentId+"'", null, null);
		return cursor;
	}
	public String getLabelsByIssueId(ContentResolver contentResolver, int gcodeId,int issueId) {
		String ret = "";
		String[] projection = {Labels.LABEL,};
		Cursor cursor = contentResolver.query(Labels.CONTENT_URI, projection, Labels.GCODE_ID + "='" + gcodeId + "' AND "+Labels.ISSUE_ID + "='"+issueId+"'", null, null);
		while (null != cursor && cursor.moveToNext()) {
			if(cursor.isLast())
				ret += cursor.getString(0);
			else
				ret += cursor.getString(0)+", ";
		}
		cursor.close();
		return ret;
	}
	// checks to see if a project with a given name and type is in our database
	public int isProjectInDB(ContentResolver contentResolver, String title, int type) {
		int ret = 0;
		String[] projection = {Projects._ID,};
		Cursor cursor = contentResolver.query(Projects.CONTENT_URI, projection, Projects.NAME + "='" + title + "' AND "+Projects.TYPE + "="+type, null, null);
		if (null != cursor && cursor.moveToNext()) {
			ret = cursor.getInt(0);
		}
		cursor.close();
		return ret;
	}
	public Cursor getProjects(ContentResolver contentResolver) {
		String[] projection = {Projects._ID,Projects.NAME,Projects.TYPE};
		Cursor cursor = contentResolver.query(Projects.CONTENT_URI, projection, null, null, null);
		return cursor;
	}
	public Cursor getInternalProjects(ContentResolver contentResolver) {
		String[] projection = {Projects._ID,Projects.NAME,Projects.TYPE};
		Cursor cursor = contentResolver.query(Projects.OLD_CONTENT_URI, projection, null, null, null);
		return cursor;
	}
	public Cursor getGoogleProjects(ContentResolver contentResolver) {
		String[] projection = {Projects._ID,Projects.NAME,};
		Cursor cursor = contentResolver.query(Projects.CONTENT_URI, projection, Projects.TYPE +"='0'", null, null);
		return cursor;
	}
	public int getGoogleIdbyProjectId(ContentResolver contentResolver,int projectId) {
		String[] projection = {GCode._ID,};
		int gcodeId = 0;
		Cursor cursor = contentResolver.query(GCode.CONTENT_URI, projection, GCode.PROJECT_ID+"='"+projectId+"'", null, null);
		if (null != cursor && cursor.moveToNext()) {
			gcodeId = cursor.getInt(0);
		}
		cursor.close();
		return gcodeId;
	}
	public int getProjectIdbyGoogleId(ContentResolver contentResolver,int gcodeId) {
		String[] projection = {GCode.PROJECT_ID,};
		int projectId = 0;
		Cursor cursor = contentResolver.query(GCode.CONTENT_URI, projection, GCode._ID+"='"+gcodeId+"'", null, null);
		if (null != cursor && cursor.moveToNext()) {
			projectId = cursor.getInt(0);
		}
		cursor.close();
		return projectId;
	}
	public String getProjectName(ContentResolver contentResolver, int id) {
		String[] projection = {Projects.NAME,};
		String name = "";
		Cursor cursor = contentResolver.query(Projects.CONTENT_URI, projection, Projects._ID + "='" + id + "'", null, null);
		if (null != cursor && cursor.moveToNext()) {
				name = cursor.getString(0);
		}
		cursor.close();
		return name;
	}
	public boolean isIssueInDB(ContentResolver contentResolver, int projId, String issueId) {
		boolean ret = false;
		Cursor cursor = contentResolver.query(Issues.CONTENT_URI, null, Issues.ISSUE_ID + "='" + issueId + "' AND "+Issues.GCODE_ID + "= '"+projId+"'", null, null);
		if (null != cursor && cursor.moveToNext()) {
			ret = true;
		}
		cursor.close();
		return ret;
	}
	public boolean isSyncedGcode(ContentResolver contentResolver, int gcodeId) {
		boolean ret = false;
		Cursor cursor = contentResolver.query(GCode.CONTENT_URI, null, GCode._ID + "='" + gcodeId + "' AND DATETIME("+GCode.LASTSYNC+") > DATETIME('now','-30 minutes') ", null, null);
		if (null != cursor && cursor.moveToNext()) {
			ret = true;
		}
		cursor.close();
		return ret;
	}
	public boolean isSyncedGcodeIssues(ContentResolver contentResolver, int gcodeId, int issueId) {
		boolean ret = false;
		Cursor cursor = contentResolver.query(Issues.CONTENT_URI, null, Issues.GCODE_ID + "='" + gcodeId + "' AND "+Issues.ISSUE_ID + "='" + issueId + "' AND DATETIME("+Issues.LASTSYNC+") > DATETIME('now','-30 minutes') ", null, null);
		if (null != cursor && cursor.moveToNext()) {
			ret = true;
		}
		cursor.close();
		return ret;
	}
	public boolean isLabelInDB(ContentResolver contentResolver, int projectId, String issueId, String label) {
		boolean ret = false;
		Cursor cursor = contentResolver.query(Labels.CONTENT_URI, null,Labels.GCODE_ID + "='" + projectId + "' AND "+ Labels.ISSUE_ID + "='" + issueId + "' AND "+Labels.LABEL + "= '"+label+"'", null, null);
		if (null != cursor && cursor.moveToNext()) {
			ret = true;
		}
		cursor.close();
		return ret;
	}

	public boolean isCommentInDB(ContentResolver contentResolver, int projId, int issueId, int commentId) {
		boolean ret = false;
		Cursor cursor = contentResolver.query(Comments.CONTENT_URI, null, Comments.ISSUE_ID + "='" + issueId + "' AND "+Comments.GCODE_ID + "= '"+projId+"' AND "+Comments.COMMENT_ID + "= '"+commentId+"'", null, null);
		if (null != cursor && cursor.moveToNext()) {
			ret = true;
		}
		cursor.close();
		return ret;
	}

	public boolean isCommentUpdateInDB(ContentResolver contentResolver, int projId, int issueId, int commentId, String content, int type) {
		boolean ret = false;
		Cursor cursor = contentResolver.query(CommentUpdates.CONTENT_URI, null, CommentUpdates.ISSUE_ID + "='" + issueId + "' AND "+CommentUpdates.GCODE_ID + "= '"+projId+"' AND "+CommentUpdates.COMMENT_ID + "= '"+commentId+"' AND "+CommentUpdates.TYPE + "= '"+type+"'  AND "+CommentUpdates.CONTENT + "= '"+content+"'", null, null);
		if (null != cursor && cursor.moveToNext()) {
			ret = true;
		}
		cursor.close();
		return ret;
	}
	
	// erases all entries in the database
	public void refreshCache(ContentResolver contentResolver) {
		contentResolver.delete(Issues.CONTENT_URI, null, null);
		contentResolver.delete(Labels.CONTENT_URI, null, null);

	}

	public Cursor getIssues(ContentResolver contentResolver, Uri projectUri, String[] projection) {
		Cursor cursor = contentResolver.query(projectUri, projection,null,null,null);
		return cursor;
	
	}
}
